package controls 
{
	import flash.display.Graphics;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.utils.Dictionary;
	import flash.utils.getTimer;
	import protocol.dataObjects.TimeBarPositionChange;
	import protocol.dataObjects.TimeBarUserPosition;
	import protocol.dataObjects.User;
	import ru.volgogradetzzz.tween.TimerTween;
	import ru.volgogradetzzz.utils.Logger;
	
	/**
	 * ...
	 * @author Volgogradetzzz
	 */
	public class TimeBar extends Sprite 
	{
		private const _BAR_Y:uint = 10;
		private const _MEDIATORS:Vector.<TimeBarMediator> = new Vector.<TimeBarMediator>();
		private const _MEDIATOR_BY_USER_ID:Dictionary = new Dictionary();
		
		private var _w:Number = 300;
		private var _bar:Shape = new Shape();
		
		public function TimeBar()
		{
			_bar.graphics.beginFill(0x00AA00);
			_bar.graphics.drawRect(0, 0, _w, 10);
			_bar.graphics.endFill();
			_bar.y = _BAR_Y;
			
			addChild(_bar);
		}
		
		public function init(users:Vector.<User>):void
		{
			for each(var user:User in users)
			{
				var icon:TimeBarIcon = new TimeBarIcon();
				//icon.alpha = 0.5;
				addChild(icon);
				
				var mediator:TimeBarMediator = new TimeBarMediator();
				mediator.init(user, icon, _w);
				
				_MEDIATOR_BY_USER_ID[user.id] = mediator;
				
				_MEDIATORS.push(mediator);
			}
		}
		
		public function start(posChange:TimeBarPositionChange):void
		{
			for each(var userPos:TimeBarUserPosition in posChange.userPositions)
			{
				var mediator:TimeBarMediator = _MEDIATOR_BY_USER_ID[userPos.userId];
				if (mediator)
				{
					TimerTween.addTween(mediator, 'position', mediator.user.timeBarPosition, userPos.newPosition, posChange.duration, null, tweenUpdateCallback);
				}
				else
				{
					Logger.getInstance().log('TimeBar.start - не найден медиатор для юзера ' + userPos.userId, Logger.ERROR);
				}
			}
		}
		
		private function tweenCmpleteCallback(mediator:TimeBarMediator):void 
		{
			
		}
		
		private function tweenUpdateCallback(mediator:TimeBarMediator):void 
		{
			mediator.update();
		}
	}
}

import flash.display.DisplayObject;
import flash.display.Sprite;
import helpers.LibraryNames;
import interfaces.combat.buttons.timeBarIcon.TimeBarIconDisabledSkin;
import interfaces.combat.buttons.timeBarIcon.TimeBarIconDownSkin;
import interfaces.combat.buttons.timeBarIcon.TimeBarIconOverSkin;
import interfaces.combat.buttons.timeBarIcon.TimeBarIconUpSkin; TimeBarIconUpSkin;
import protocol.dataObjects.User;
import ru.volgogradetzzz.controls.AnimatedButton;
import ru.volgogradetzzz.utils.LibraryExplorer;

internal class TimeBarIcon extends AnimatedButton
{
	public function TimeBarIcon()
	{
		var up:TimeBarIconUpSkin = LibraryExplorer.getInstance().getObject(LibraryNames.INTERFACE, 'timeBarIconUpSkin', true);
		var over:TimeBarIconOverSkin = LibraryExplorer.getInstance().getObject(LibraryNames.INTERFACE, 'timeBarIconOverSkin', true);
		var down:TimeBarIconDownSkin = LibraryExplorer.getInstance().getObject(LibraryNames.INTERFACE, 'timeBarIconDownSkin', true);
		var disabled:TimeBarIconDisabledSkin = LibraryExplorer.getInstance().getObject(LibraryNames.INTERFACE, 'timeBarIconDisabledSkin', true);
		
		up.iconPlaceholder.addChild(LibraryExplorer.getInstance().getObject(LibraryNames.DUMMIES, 'dummyIcon', true) as DisplayObject);
		over.iconPlaceholder.addChild(LibraryExplorer.getInstance().getObject(LibraryNames.DUMMIES, 'dummyIcon', true) as DisplayObject);
		down.iconPlaceholder.addChild(LibraryExplorer.getInstance().getObject(LibraryNames.DUMMIES, 'dummyIcon', true) as DisplayObject);
		disabled.iconPlaceholder.addChild(LibraryExplorer.getInstance().getObject(LibraryNames.DUMMIES, 'dummyIcon', true) as DisplayObject);
		
		var hitArea:Sprite = new Sprite();
		hitArea.graphics.beginFill(0x000000);
		hitArea.graphics.drawCircle(15, 15, 15);
		hitArea.graphics.endFill();
		
		super(up, over, down, disabled);
		setHitArea(hitArea);
	}
}

internal class TimeBarMediator
{
	public var user:User;
	public var icon:TimeBarIcon;
	public var position:Number;
	
	private var _timeBarWidth:Number;
	
	public function init(user:User, icon:TimeBarIcon, timeBarWidth:Number):void
	{
		this.user = user;
		this.icon = icon;
		_timeBarWidth = timeBarWidth;
		
		position = user.timeBarPosition;
		
		icon.x = _timeBarWidth - _timeBarWidth * position / User.TIME_BAR_POSITION_PRECISION;
	}
	
	public function update():void
	{
		user.timeBarPosition = position;
		icon.x = _timeBarWidth - _timeBarWidth * position / User.TIME_BAR_POSITION_PRECISION;
	}
	
	public function clear():void
	{
		user = null;
		icon = null;
	}
}